package com.project.housesalesystem.config;

import com.project.housesalesystem.annotation.IgnoreAuth;

<<<<<<< HEAD
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
=======
>>>>>>> 66dcee4e153c16ef05fd0c7e1ed9f414a0bf0bc9
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

<<<<<<< HEAD

=======
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
>>>>>>> 66dcee4e153c16ef05fd0c7e1ed9f414a0bf0bc9

/**
 * 权限(Token)验证
 */
@Component
public class AuthorizationInterceptor implements HandlerInterceptor {

//    public static final String LOGIN_TOKEN_KEY = "Token";

    
<<<<<<< HEAD
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
=======
	@Override

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


>>>>>>> 66dcee4e153c16ef05fd0c7e1ed9f414a0bf0bc9
        String servletPath = request.getServletPath();
        if ("/dictionary/page".equals(request.getServletPath()) || "/file/upload".equals(request.getServletPath()) || "/yonghu/register".equals(request.getServletPath())) {//请求路径是字典表或者文件上传 直接放行
            return true;
        }
        //支持跨域请求
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,request-source,Token, Origin,imgType, Content-Type, cache-control,postman-token,Cookie, Accept,authorization");
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

        IgnoreAuth annotation;
        if (handler instanceof HandlerMethod) {
            annotation = ((HandlerMethod) handler).getMethodAnnotation(IgnoreAuth.class);
        } else {
            return true;
        }

        //从header中获取token
//        String token = request.getHeader(LOGIN_TOKEN_KEY);

        /**
         * 不需要验证权限的方法直接放过
         */
        if (annotation != null) {
            return true;
        }
        return true;
    }
//
//        TokenEntity tokenEntity = null;
//        if(StringUtils.isNotBlank(token)) {
//        	tokenEntity = tokenService.getTokenEntity(token);
//        }
//
//        if(tokenEntity != null) {
//        	request.getSession().setAttribute("userId", tokenEntity.getUserid());
//        	request.getSession().setAttribute("role", tokenEntity.getRole());
//        	request.getSession().setAttribute("tableName", tokenEntity.getTablename());
//        	request.getSession().setAttribute("username", tokenEntity.getUsername());
//        	return true;
//        }
//
//		PrintWriter writer = null;
//		response.setCharacterEncoding("UTF-8");
//		response.setContentType("application/json; charset=utf-8");
//		try {
//		    writer = response.getWriter();
//		    writer.print(JSONObject.toJSONString(R.error(401, "请先登录")));
//		} finally {
//		    if(writer != null){
//		        writer.close();
//		    }
//		}
////				throw new EIException("请先登录", 401);
//		return false;
//    }
}
